OpenRoads Designer CONNECT Edition SDK Help

Delete cell from cell library

The below code snippet shows deleting the cell from the cell library using the cell name. It opens the cell library as Dgn, finds the cell model, deletes it, and saves the .cel file.


//Required References
using System;
using System.Diagnostics;
using Bentley.MstnPlatformNET;
using Bentley.DgnPlatformNET;

public bool DeleteCellFromLibrary()
        {
            //Set the cell name to delete from library
            string cellNameToDelete = "3D Cable Barrier v2 - Left Trailing End";
            DgnModel cellModel = null;
            try
            {
                //Get all cells 
                var libs = new CellLibraryCollection(CellLibraryOptions.DefaultAll);
                foreach (var lib in libs)
                {
                    //Find the cell library using cell name
                    if (lib.Name == cellNameToDelete)
                    {
                        //Open the cell library file in Read and Write mode and get the DgnFile object 
                        Bentley.DgnPlatformNET.DgnFile cellLibraryFile;
                        String cellLibraryFilePath = lib.File.GetFileName();
                        Bentley.DgnPlatformNET.DgnDocument dgnDocumentObj = DgnDocument.CreateForLocalFile(cellLibraryFilePath);
                        Bentley.DgnPlatformNET.DgnFileOwner DgnFileOwnerObj = DgnFile.Create(dgnDocumentObj, DgnFileOpenMode.ReadWrite);
                        cellLibraryFile = DgnFileOwnerObj.DgnFile;

                        StatusInt openForReadWriteStatus;
                        Bentley.DgnPlatformNET.DgnFileStatus FileStatus;

                        //Load the dgn file into current session
                        if (DgnFileStatus.Success != (FileStatus = cellLibraryFile.LoadDgnFile(out openForReadWriteStatus)))
                            return false;

                        //Find the cell model from the Dgn file
                        cellModel = cellLibraryFile.LoadRootModelById(out StatusInt status, lib.File.FindModelIdByName(lib.Name), true, true, true);

                        //delete model from Dgn(.cel file)
                        StatusInt statusdelete = cellLibraryFile.DeleteModel(cellModel);
                        if (StatusInt.Success == statusdelete)
                        {
                            //Save the cell library Dgn file
                            cellLibraryFile.ProcessChanges(DgnSaveReason.FileClose);
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return false;
        }